using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LayUI.Web.Controllers
{
    /// <summary>
    /// 参考网址：  https://layuion.com/
    /// 
    /// 1111   北风那个吹。。
    /// 
    /// 
    /// dev--211111
    /// 
    /// 。 
    /// </summary>
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Default()
        {
            return View();
        }

        /// <summary>
        /// public JsonResult GetMenu()  core中返回的是 IActionResult 类型
        /// </summary>
        /// <returns></returns>
        public JsonResult GetMenu()
        {
            //方式一： 死数据
            #region
            var list = new List<Menu>();
            var menus = new List<Menu>
            {
                new Menu
                {
                    Id = 1,
                    Title = "系统首页",
                    Url = "/permission/index",
                    State = true,
                    Childrens = new List<Menu>()
                },
                new Menu
                {
                    Id = 21,
                    Title = "2112.NetA",
                    Url = "/permission/index",
                    State = true,
                    Childrens = new List<Menu>()
                    {
                        new Menu
                        {
                            Id = 211,
                            Title = "张铎小组",
                            Url = "/test/test1",
                            State = true,
                            Childrens = new List<Menu>() { }
                        },
                        new Menu
                        {
                            Id = 212,
                            Title = "海通小组",
                            Url = "/test/default",
                            State = true,
                            Childrens = new List<Menu>() { }
                        },
                    }
                },
                new Menu
                {
                    Id = 1,
                    Title = "系统设置",
                    Url = "",
                    State = true,
                    Childrens = new List<Menu>
                    {
                        new Menu
                        {
                            Id = 2,
                            Title = "权限管理",
                            Url = "/permission/index",
                            State = true,
                            Childrens = new List<Menu>()
                        },
                        new Menu
                        {
                            Id = 3,
                            Title = "角色管理",
                            Url = "/permission/role",
                            State = true,
                            Childrens = new List<Menu>()
                        },
                        new Menu
                        {
                            Id = 4,
                            Title = "人员管理",
                            Url = "/permission/person",
                            State = true,
                            Childrens = new List<Menu>()
                        }
                    }
                },
                new Menu
                {
                    Id = 5,
                    Title = "天王盖地虎",
                    Url = "",
                    State = true,
                    Childrens = new List<Menu>
                    {
                        new Menu
                        {
                            Id = 6,
                            Title = "桥如虹，水如空",
                            Url = "",
                            State = true,
                            Childrens = new List<Menu>
                            {
                                new Menu
                                {
                                    Id = 6,
                                    Title = "澹台灭明",
                                    Url = "/permission/index",
                                    State = true,
                                    Childrens = new List<Menu>()
                                },
                                new Menu
                                {
                                    Id = 7,
                                    Title = "庄子试妻",
                                    Url = "/permission/role",
                                    State = true,
                                    Childrens = new List<Menu>()
                                },
                                new Menu
                                {
                                    Id = 8,
                                    Title = "九合诸侯",
                                    Url = "/permission/person",
                                    State = true,
                                    Childrens = new List<Menu>()
                                }
                            }
                        },
                        new Menu
                        {
                            Id = 7,
                            Title = "与君初相识",
                            Url = "/permission/role",
                            State = true,
                            Childrens = new List<Menu>()
                        },
                        new Menu
                        {
                            Id = 8,
                            Title = "犹如故人归",
                            Url = "/permission/person",
                            State = true,
                            Childrens = new List<Menu>()
                        }
                    }
                }
            };

            #endregion

            ////方式二： 从数据库读取权限
            //var menus = Test();

            return Json(menus, JsonRequestBehavior.AllowGet);
        }

        public List<Menu> Test()
        {
            //模拟从数据库取数据集合
            var list = GetPermissions();
            var tmpList = list.Where(m => m.PId == 0).ToList();

            var menus = new List<Menu>();
            for (int i = 0; i < tmpList.Count; i++)
            {
                var menu = new Menu();
                menu.Id = tmpList[i].Id;
                menu.Title = tmpList[i].Name;
                menu.Url = tmpList[i].Url;
                menu.State = list[i].State;
                menu.Childrens = GetChildrens(list, menu.Id);
                menus.Add(menu);
            }

            return menus;
        }

        /// <summary>
        /// 递归算法  : 指在当前方法内调用自己的这种现象。
        /// </summary>
        /// <param name="permissions"></param>
        /// <param name="pId"></param>
        /// <returns></returns>
        public List<Menu> GetChildrens(List<Permission> permissions, int pId)
        {
            var menus = new List<Menu>();
            var tmpList = permissions.Where(m => m.PId == pId);
            if (!tmpList.Any())
                return menus;

            var list = tmpList.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                var menu = new Menu();
                menu.Id = list[i].Id;
                menu.Title = list[i].Name;
                menu.Url = list[i].Url;
                menu.State = list[i].State;
                menu.Childrens = GetChildrens(permissions, menu.Id);
                menus.Add(menu);
            }

            return menus;
        }

        public List<Permission> GetPermissions()
        {
            //5表联查，取出当前用户对应的角色-所分配的权限集合
            return new List<Permission>
            {
                new Permission
                {
                    Id = 1,
                    PId = 0,
                    Name = "系统管理",
                    Url = "",
                    State = true
                },
                new Permission
                {
                    Id = 2,
                    PId = 0,
                    Name = "基础数据",
                    Url = "",
                    State = true
                },
                new Permission
                {
                    Id = 3,
                    PId = 1,
                    Name = "权限管理",
                    Url = "/permission/index",
                    State = true
                },
                new Permission
                {
                    Id = 4,
                    PId = 1,
                    Name = "角色管理",
                    Url = "/permission/Role",
                    State = true
                },
                new Permission
                {
                    Id = 5,
                    PId = 1,
                    Name = "用户管理",
                    Url = "/permission/Role",
                    State = true
                },
                new Permission
                {
                    Id = 6,
                    PId = 2,
                    Name = "公共字典",
                    Url = "/permission/index",
                    State = true
                },
                new Permission
                {
                    Id = 7,
                    PId = 2,
                    Name = "日志管理",
                    Url = "/permission/index",
                    State = true
                },
                 new Permission
                {
                    Id = 7,
                    PId = 2,
                    Name = "月上柳梢头",
                    Url = "/permission/index",
                    State = true
                }
            };
        }
    }
}
